using System;
using Microsoft.AspNetCore.Mvc;
using ApiProject.Api.Entity;
using System.Linq;
using ApiProject.Api.Repository;
using Microsoft.Extensions.Configuration;
using ApiProject.Api.Uitls;
using Microsoft.AspNetCore.Hosting;
using ApiProject.Api.Utils;
using ApiProject.Api.PramaModel;
using System.Collections;


// 文章阅读管理
namespace ApiProject.Api.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class ArticleReadController : ControllerBase
    {
        private IRepository<Articles> _articleRepository;
        private IRepository<Comments> _commentRepository;
        private IRepository<Users> _userRepository;
        private IRepository<ReadQuantities> _reqRepository;
        private IRepository<UserInfo> _userInfoRepository;


        public ArticleReadController(

            IRepository<Articles> articleRepository,
            IRepository<Comments> commentRepository,
            IRepository<Users> userRepository,
            IRepository<ReadQuantities> reqRepository,
            IRepository<UserInfo> userInfoRepository
        )
        {
            _articleRepository = articleRepository;
            _reqRepository = reqRepository;
            _userRepository = userRepository;
            _commentRepository = commentRepository;
            _userInfoRepository = userInfoRepository;
        }

        // 客户阅读点击
        [HttpPut("{id}")]
        public dynamic ChangeRead(int id)
        {
            //接收用户Id
            // var userId = model.UserId;

            //通过文章id查找文章
            var article = _articleRepository.GetById(id);

            //调用方法则阅读总量加一
            article.SumRead += 1;
            article.UpdatedTime = DateTime.Now;
            _articleRepository.Update(article);

            //添加阅读量表数据
            // var req = new ReadQuantities
            // {
            //     ArticleId = id,
            //     UserId = userId
            // };
            // _reqRepository.Insert(req);

            return JsonHelper.Serialize(new
            {
                Code = 200,
                Data = article,
                Msg = string.Format("成功添加Id为{0}的文章阅读量", id)
            });

        }

        //添加评论内容
        [HttpPost, Route("com")]
        public dynamic AddCom(NewCom model)
        {
            var userId = model.UserId;
            var articleId = model.ArticleId;
            var comment = model.Comment;

            var com = new Comments
            {
                UserId = userId,
                ArticleId = articleId,
                Comment = comment
            };
            _commentRepository.Insert(com);

            //评论总量加一
            var byIdArticle=_articleRepository.GetById(articleId);
            byIdArticle.SumCom+=1;
            byIdArticle.UpdatedTime=DateTime.Now;
            _articleRepository.Update(byIdArticle);

            //通过当前文章Id查找所有评论
            var coms = _commentRepository.Table.Where(x => x.ArticleId == articleId).ToList();
            //新建List空数组
            ArrayList arraylist = new ArrayList();

            var user = _userRepository.GetById(userId);
            var newobj = new
            {
                UserId = com.UserId,
                NewUsername = user.Username,
                ComConent = com.Comment,
                ArticleId = com.ArticleId
            };

            arraylist.Add(newobj);

            return JsonHelper.Serialize(new
            {
                Code = 200,
                Data = arraylist,
                Msg = string.Format("成功添加评论内容")
            });
        }

        //查找所有评论并返回对应用户Id的用户名
        [HttpGet, Route("art/{id}")]
        public dynamic GetAllCom(int id)
        {
            //通过当前文章Id查找所有评论
            var coms = _commentRepository.Table.Where(x => x.ArticleId == id).ToList();

            // var users = _userRepository.Table.ToList();
            //新建List空数组
            ArrayList arraylist = new ArrayList();

            coms.ForEach(item =>
            {
                var user = _userInfoRepository.GetById(item.UserId);
                var newobj = new
                {
                    UserId = item.UserId,
                    NewUsername = user.NickName,
                    ComConent = item.Comment,
                    ArticleId = item.ArticleId
                };
                arraylist.Add(newobj);
            });


            return JsonHelper.Serialize(new
            {
                Code = 200,
                Data = arraylist,
                Msg = string.Format("查找成功")
            });
        }













    }



}